package com.example.androidlearning.sy9;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

import com.example.androidlearning.R;

public class Sy0901_Frame extends AppCompatActivity {

    ImageView view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sy0901_frame);
        Button play = findViewById(R.id.sy09_play);
        Button pause = findViewById(R.id.sy09_pause);
        view = findViewById(R.id.sy09_anim);
        final AnimationDrawable anim = (AnimationDrawable)view.getBackground();
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                anim.start();
            }
        });
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                anim.stop();
            }
        });
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alphaImpl(v);
            }
        });
    }

    //补间动画
    public void alphaImpl(View v){
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
        v.startAnimation(animation);
    }

    public void rotateImpl(View v){
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
        v.startAnimation(animation);
    }
    //属性动画
    public void E91Salpha(View v){
        ObjectAnimator alphaAni = ObjectAnimator.ofFloat(view,"alpha",1,0);
        alphaAni.setDuration(1000);
        alphaAni.start();
    }
    public void E91Srotate(View v){
        ObjectAnimator rotateAni = ObjectAnimator.ofFloat(view,"rotation",0,360);
        rotateAni.setDuration(1000);
        rotateAni.start();
    }
    public void E91Sscale(View v){
        ObjectAnimator scaleAniX = ObjectAnimator.ofFloat(view,"scaleX",1,1.1f,1.1f,1);
        scaleAniX.setDuration(1000);
        scaleAniX.setRepeatCount(-1);
        scaleAniX.setRepeatMode(ValueAnimator.RESTART);

        ObjectAnimator scaleAniY = ObjectAnimator.ofFloat(view,"scaleY",1,1.1f,1.1f,1);
        scaleAniY.setDuration(1000);
        scaleAniY.setRepeatCount(-1);
        scaleAniY.setRepeatMode(ValueAnimator.RESTART);

        AnimatorSet aSet = new AnimatorSet();
        aSet.playTogether(scaleAniX,scaleAniY);
        aSet.start();
    }
    public void E91Stranslate(View v){
        ObjectAnimator translationX = ObjectAnimator.ofFloat(view,"translationX",0,600f);
        ObjectAnimator translationY = ObjectAnimator.ofFloat(view,"translationY",0,0);

        AnimatorSet animatorSet = new AnimatorSet();  //组合动画
        animatorSet.playTogether(translationX,translationY); //设置动画
        animatorSet.setDuration(3000);  //设置动画时间
        animatorSet.start(); //启动
    }

    public void E91SAll(View v){
        ObjectAnimator alphaAni = ObjectAnimator.ofFloat(view,"alpha",1,0);
        alphaAni.setDuration(1000);

        ObjectAnimator rotateAni = ObjectAnimator.ofFloat(view,"rotation",0,360);
        rotateAni.setDuration(1000);

        ObjectAnimator scaleAniX = ObjectAnimator.ofFloat(view,"scaleX",1,1.1f,1.1f,1);
        scaleAniX.setDuration(1000);
        scaleAniX.setRepeatCount(-1);
        scaleAniX.setRepeatMode(ValueAnimator.RESTART);

        ObjectAnimator scaleAniY = ObjectAnimator.ofFloat(view,"scaleY",1,1.1f,1.1f,1);
        scaleAniY.setDuration(1000);
        scaleAniY.setRepeatCount(-1);
        scaleAniY.setRepeatMode(ValueAnimator.RESTART);

        ObjectAnimator translationX = ObjectAnimator.ofFloat(view,"translationX",0,600f);
        ObjectAnimator translationY = ObjectAnimator.ofFloat(view,"translationY",0,0);

        AnimatorSet animatorSet = new AnimatorSet();  //组合动画
        animatorSet.playTogether(translationX,translationY,alphaAni,rotateAni,scaleAniX,scaleAniY); //设置动画
        animatorSet.setDuration(3000);  //设置动画时间
        animatorSet.start(); //启动
    }

}
